Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Proxy for Fetch in download.ts - Resolves issue 1280 #1291

Merged
merged 14 commits into from
May 20, 2021

Conversation

cdesch
Copy link
Contributor

@cdesch cdesch commented Mar 2, 2021

Added functionality to allow HTTP_PROXY and HTTPS_PROXY environment variables for proxy settings.

This addresses the issue#1280 to be able to download the compilers behind a corporate firewall via a proxy.

This pull request also adds test cases packages/hardhat-core/test/internal/util/download.ts for the existing functionality without using a proxy and using a proxy.

@cdesch
Copy link
Contributor Author

cdesch commented Mar 2, 2021

I feel that the Windows CI Build is failing across the board as noted with PR #1268

@cdesch
Copy link
Contributor Author

cdesch commented Mar 3, 2021

It would be mighty cool if this PR was checked out.... might help some us behind proxies use HardHat instead of the TruffleSuite.

😁

@alcuadrado
Copy link
Member

Hey @cdesch, thanks for submitting this PR. We'll review it in the next few days or early next week.

@cdesch
Copy link
Contributor Author

cdesch commented Mar 4, 2021

Thank you. Surely it will make HardHat a little more accessible :-p

@cdesch
Copy link
Contributor Author

cdesch commented Mar 15, 2021

@alcuadrado is there anything I can do to assist with getting this pull request merged? :bowtie:

@fvictorio
Copy link
Member

Hey @cdesch, sorry for not having reviewed this yet. I'll take a look this week.

@cdesch
Copy link
Contributor Author

cdesch commented Mar 16, 2021

@fvictorio Thanks. Let me know if there is anything I can do to help. I'm happy to revise or making another PR if you have feedback. There can be some fun issues that crop up with proxies and it is kind of an edge case for some folks, which is why I included tests. I worked a little bit on adding custom certificates for the proxy but it seems that isn't needed currently.

I used ENV variables in my commit as it is a pretty standard process for most proxies and the same as when using a proxy for npm. hardhat.config.js didn't feel like the right place, but you may have a different opinion about including proxy configs there.

@fvictorio
Copy link
Member

I used ENV variables in my commit as it is a pretty standard process for most proxies and the same as when using a proxy for npm. hardhat.config.js didn't feel like the right place, but you may have a different opinion about including proxy configs there.

That seems fine to me, there seems to be a convention around those two environment variables and as far as I can tell the right place to handle them in this scenario is in Hardhat (there's an issue about these variables in node-fetch but they decided that it should be done at the application level).

@fvictorio
Copy link
Member

@cdesch Thanks a lot for this. I made some minor changes. I'll add some comments explaining them in case you are interested 🙂


const response = await fetch(url, { timeout: timeoutMillis });
if (process.env.HTTPS_PROXY !== undefined) {
fetchOptions.agent = new HttpsProxyAgent(process.env.HTTPS_PROXY);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was some unnecessary casting here. The properties of process.env have type string | undefined, so if you check for that in the if, typescript will know that the type is string inside the block.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Yeah, I was struggling a bit on the TypeScript learning curve, but I'm slowly migrating a project to TypeScript currently.

@@ -1,5 +1,7 @@
import fs from "fs";
import fsExtra from "fs-extra";
import { HttpsProxyAgent } from "https-proxy-agent";
import type { RequestInit as FetchOptions } from "node-fetch";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Manually defining FetchOptions wasn't necessary, it is better to import it from the module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. I think I was a bit confused on that because of the const { default: fetch } = await import("node-fetch"); which seemed strange as to where to put the import. Thank you!

// Setup Proxy Server
proxy = new Proxy();
connections = 0;
proxy.on("connection", () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this so we can assert that the proxy is really being used.

process.env.HTTPS_PROXY = oldHttpsProxyEnv;
} else {
delete process.env.HTTPS_PROXY;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To save the current environment, you were doing something like this:

env = process.env
process.env.HTTP_PROXY = ...

But since process.env is an object, any modifications you were doing to it were also done to env, so nothing was restored afterwards.

I also have to explicitly use delete here because it turns out that

process.env.FOO = undefined

doesn't remove the FOO environment variable. Instead, it sets it to the string "undefined". Terrible behavior from node, but it is what it is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. I think adapted that portion from the https-proxy-agent package, and in fact I wasn't sure if the env really needed to be changed back at the end of the test because I wasn't sure if changes to the process.env were scoped to the test or not. It seems like they are.

Thanks for catching this.

@cdesch
Copy link
Contributor Author

cdesch commented Mar 19, 2021

Thank you @fvictorio for the comments and the feedback! I learned a lot in the process to get this working.

@cdesch
Copy link
Contributor Author

cdesch commented Mar 26, 2021

@fvictorio Do you think this PR will make the next HH release? Possibly 2.2.x or minor like 2.1.3?

Would you like me to add docs for this?

@alcuadrado
Copy link
Member

Hey @cdesch, thanks for this PR. Unfortunately we have to focus on implementing Berlin right now, so we won't get merged this in the next couple of weeks. Once the new hardfork support is out, we'll review and release this.

@cdesch
Copy link
Contributor Author

cdesch commented Apr 2, 2021

Thanks @alcuadrado, Can you recommend how I can install the NPM package from github in the meantime? I understand that typically you can do a npm install https://github.com/<username>/<repository> but since the HardHat repo has multiple packages in it under packages/* such as packages/hardhat-core, etc... How would I install just one or more of them manually until the next release?

My apologies for being persistent, this issue is a show stopper for my team using hardhat on a new project, but we understand that you have priorities too.

@alcuadrado
Copy link
Member

I'm afraid it can't be installed like that. Check the contributing.md which explains how to build it locally.

@cdesch
Copy link
Contributor Author

cdesch commented Apr 14, 2021

@alcuadrado Okay. Thank you.

@cdesch
Copy link
Contributor Author

cdesch commented May 2, 2021

@alcuadrado and @fvictorio Happy Weekend! Any chance of looking at this PR this week? 😇

@cdesch
Copy link
Contributor Author

cdesch commented May 4, 2021

I updated this PR to the current Master branch and attempted to resolve the merge conflict with yarn.lock.

The test suite fails with:

  1 failing

  1) Eth module
       JSON-RPC provider
         eth_sendTransaction
           return txHash
             Should return the hash of a reverted transaction:
     TypeError: Cannot read property 'txHash' of undefined
      at Context.<anonymous> (test/internal/hardhat-network/provider/modules/eth.ts:4005:39)
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (node:internal/process/task_queues:94:5)

but that seems to be from 0a575a38a and PR 1425, although I could be wrong. The error does not appear to be related to this PR.

@cdesch
Copy link
Contributor Author

cdesch commented May 16, 2021

Checking in to see if this can be merged in the next version. Is there anything I can do to help merge this in?

@alcuadrado
Copy link
Member

Hey @cdesch! I'm really sorry I took so long to review this. Thanks for submitting this PR, and for being patient.

As I read that you were struggling a bit with ts, I pushed this commit improving a few tiny bits, which you may find interesting.

I also added some docs about this. I wasn't sure where to place this info, so I added a warning box to the compilation guide for now. What do you think, @fvictorio?

Copy link
Contributor Author

@cdesch cdesch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@cdesch
Copy link
Contributor Author

cdesch commented May 19, 2021

Thank you @alcuadrado! I think everything looks good. I will test as soon as the package is published.

@alcuadrado
Copy link
Member

@fvictorio if you think the docs change is ok we can merge this.

@fvictorio fvictorio merged commit 677099d into NomicFoundation:master May 20, 2021
@fvictorio
Copy link
Member

Thanks @cdesch and sorry for the huge delay!

@cdesch
Copy link
Contributor Author

cdesch commented May 20, 2021

No worries, I completely understand! Thank you @fvictorio ! I'm ready to test it out!

@hygkui
Copy link

hygkui commented May 2, 2022

npx hardhat --show-stack-traces --verbose compile can show the download url, you can download and move it to the right dir such like '$HOME/Library/Caches/hardhat-nodejs/compilers/macosx-amd64/solc-macosx-amd64-v0.x.x+commit.fc410830', and it can be sloved the compiler download issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants